<%@ include file="header.jsp" %>
<%@page import="com.googlecode.jwsm.*"%>
<%@page import="javax.servlet.*" %>
<%@page import="org.jcommon.util.StringUtilities"%>
<%
	if (!user.hasPrivilege("jwsm.service_manage")) {
		response.sendRedirect("index.jsp?error=Insufficient privileges to modify settings");
	}

	String message = null;
	String error = null;

	java.util.prefs.Preferences prefs = com.googlecode.jwsm.Uploader.prefs;
	prefs.sync();
	
	String password = prefs.get("serverPassword", "");
	int length = password.length();
	StringBuffer buffer = new StringBuffer();
	for (int i = 0; i < length; i++) {
		buffer.append('*');
	}
	if (request.getParameter("deployments") != null) {
		prefs.put("deploymentDirectory", request.getParameter("deployments"));
		prefs.put("loggingEnabled", request.getParameter("loggingEnabled"));
		prefs.put("serverName", request.getParameter("serverName"));
		prefs.flush();
		try {
			com.googlecode.jwsm.Uploader.reinit(false);
		} catch(Throwable t) {
			throw new ServletException(t);
		}
		message = "Update successful, JWSM restarted";
	} else if ("purge".equals(request.getParameter("logs"))) {
		message = "Purged " + DatabaseLogHandler.purgeLogs() + " log entries";
	} else if ("delete".equals(request.getParameter("logs"))) {
		/*if (DatabaseLogHandler.deleteDatabase()) {
	message = "Deleted database";
		} else {
	error = "An error occurred while attempting to delete the database";
		}*/
	}
%>
<%
	if (message != null) {
%>

<b style="color: blue;"><%=message%></b><br/><br/>
<%
	}
%>
<%
	if (error != null) {
%>
	<b style="color: red;"><%=error%></b><br/><br/>
<%
	}
%>
<%
	if (Uploader.initStackTrace != null) {
%>
	<b style="color: red;">An error occurred at startup, stacktrace follows:</b>
	<table width="500" cellpadding="0" cellspacing="0" border="0"><tr><td><%=StringUtilities.getHTMLStackTrace(Uploader.initStackTrace)%></td></tr></table><br/>
<%
	}
%>
<form method="post">
<table cellpadding="10" cellspacing="0" class="table">
	<tr>
		<td align="center" bgcolor="<%= CSSDefaults.BG_COLOR %>" colspan="2"><span style="color: white; font-size: 14pt; font-weight: bold;">Server Configuration</span></td>
	</tr>
	<tr id="DeploymentDirectory">
		<td align="right"><b>Deployment Directory:</b></td>
		<td><input type="text" id="deployments" name="deployments" value="<%=com.googlecode.jwsm.Uploader.getDirectoryString()%>"/></td>
	</tr>
	<tr id="EnableLogging">
		<td align="right"><b>Enable Logging:</b></td>
		<td><select name="loggingEnabled"><option value="false">Disabled</option><option value="true"<% if (prefs.getBoolean("loggingEnabled", false)) { %> selected="selected"<% } %>>Enabled</option></select></td>
	</tr>
	<tr id="ServerName">
		<td align="right"><b>Server Name:</b></td>
		<td><input type="text" name="serverName" value="<%= prefs.get("serverName", "DefaultServer") %>"/></td>
	</tr>
	<tr valign="top">
		<td align="right"><b>Log Management:</b></td>
		<td>
			<input id="PurgeLogs" type="button" value="Purge Logs" onclick="if (confirm('Are you sure you want to delete all log entries?')) { document.location = '?logs=purge'; }"/><br/>
			<!--<input id="DeleteDatabase" type="button" value="Delete Database" onclick="if (confirm('Are you sure you want to delete the database?')) { document.location = '?logs=delete'; }"/>-->
		</td>
	</tr>
	<tr>
		<td align="center" colspan="2" id="Update"><input type="submit" value="Update"/></td>
	</tr>
</table>
</form>
<%@ include file="footer.jsp" %>